import {request,getURLParameters} from '@dashboard/api'
import {getComponent } from '@dashboard/api/src/component'
//获取到到组件数据存全局变量
(window as any).api = {
    // 组件数据
    component:[],
    // 组件类别
    compCategory:null,
    //模板数据
    template:[],
    //模板类别
    templateCategory:null,
}
//获取组件类别
const api = (window as any).api;
api.compCategory = request([
    {
        tag:'sel',
        tbAlias:'T1',
        tb:'Pub_DashBoardCompCategory',
        field:'T1.*,T2.Pub_DashBoardComponentOID',
        n_ob:'T1.Sort',
        tol:1000,
        jo:[
            {
                tableName:'Pub_DashBoardComponent',
                alias:'T2',
                on:'T1.Pub_DashBoardCompCategoryOID = T2.Pub_DashBoardCompCategory_FK',
            }
        ],
        wh:{
            Filter:'T2.Device like {device}',
            Param:{
                device:getURLParameters('device','pc')
            }
        }
    }
]).then(res=>res.data.Pub_DashBoardCompCategory.Items).then(res=>{
    api.component = [];
    let ComponentOID = res.reduce((a,b)=>{
        if(b.Pub_DashBoardComponentOID)a.push(b.Pub_DashBoardComponentOID);
        return a;
    },[])
    for (const name of ComponentOID) {
        api.component.push({
            name,
            data:getComponent(name).then(e=>({
                    name:e.Pub_DashBoardComponentOID,
                    title:e.ComponentName_CN,
                    code:e.Code,
                    componentName:e.ComponentName,
                    device:e.Device,
                    imagePath:e.ImagePath,
                    isHide:e.IsHide,
                    sort:e.Sort,
                    parentOID:e.Pub_DashBoardCompCategory_FK
                }
            ))
        })
    }
    let oid = 'Pub_DashBoardCompCategoryOID';
    return res.reduce((a:any,b:any)=>{
        if(!a.filter((e:any)=>e[oid]===b[oid]).length)a.push(b);
        return a;
    },[]).map((e:any)=>({name:e[oid],title:e.CategoryName,sort:e.Sort}));
})
api.templateCategory = request([
    {
        tag:'sel',
        tb:'Pub_ParamTemplate',
        field:'Pub_ParamTemplateOID',
        tol:1000,
        wh:{
            Filter:"Category like {Category}",
            Param:{Category:'dashboard'}
        }
    }
]).then(res=>res.data.Pub_ParamTemplate.Items).then(async res=>{
    res.forEach(async({Pub_ParamTemplateOID})=>{
        let data = await request([{
            tag:'sel',
            tb:'Pub_ParamTemplate',
            field:'Config,BusinessName',
            tol:1,
            wh:{
                Filter:'Pub_ParamTemplateOID = {Pub_ParamTemplateOID}',
                Param:{Pub_ParamTemplateOID}
            }
        }]).then(e=>e.data.Pub_ParamTemplate.Items[0])
        try{
            data.Config = JSON.parse(data.Config)
        }catch{
            data.Config = {}
            console.log(`【${data.BusinessName}】模板配置数据格式错误，默认值为{}`);
        }
        let property = data.Config.Property;
        for(let key in property){
            property[key] = property[key].filter(e=>!e.disableDashboard)
        }
        api.template.push(data)
    })
})